<?php
class category extends baseController {    
	public function __construct() {
		parent::__construct ();
	}
    
    public function loadView($temp) {
        if (!isset($temp['data']))
            $temp['data'] = '';
        $this->load->view('index', $temp);
    }
    
    public function index($slug) {
        $this->_data['template'] = 'category';
        $cate = $this->categoryModel->read(array('slug' => $slug));
        if(!$cate) redirect(base_url());
        if($slug=='mang-xa-hoi') redirect(base_url().'question');
        
        $this->session->set_userdata('selectedcategory', $cate['id']);
        
        $this->_data['category'] = $cate;
        $this->_data['subs'] = $this->getSubs($cate['id']);
        $this->_data['data'] = $this->getEntryNearlyForAllSub($cate['id']);
        
        $this->loadView($this->_data);
    }
    
    public function getSubs($parentid) {
        return $this->categoryModel->get_order_where(null, 'number', 'asc', array('parentid' => $parentid));
    }
    
    public function getEntryNearlyForSub($subid) {
        return $this->entryModel->get_order_where(10, 'id', 'desc', array('categoryid' => $subid));
    }
    
    public function getEntryNearlyForAllSub($parentid) {
        $result = array();
        $subs = $this->getSubs($parentid);
        foreach($subs as $key=>$value) {
            $entries = $this->getEntryNearlyForSub($value['id']);
            $result[$value['categoryname']] = $entries;
        }
        return $result;
    }
}
?>